package com.fyzk.activity.user;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppToast;
import com.fyzk.interfaces.onTopTitleClickListener;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.regex.Pattern;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * 完善用户信息
 *
 * @author fanxs11
 */
public class PerfectInforActivity extends BaseActivity implements onTopTitleClickListener {

    private TopTitleView topTitleView;

    //中间 center —— 身份证号
    private EditText center_shenFenZheng;
    //中间 center —— 真实姓名
    private EditText center_realName;
//    // 中间 center —— 昵称
//    private EditText center_nickname;

    private TextView tiJiao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.perfectinfor);
        initView();
    }

    private void initView() {
        topTitleView = (TopTitleView) findViewById(R.id.perfect_top_rl);
        topTitleView.setCenterText(R.string.perfect_top_title);
        topTitleView.setRightVisible(View.INVISIBLE);
        topTitleView.setOnTitleClickListener(this);

        center_shenFenZheng = (EditText) findViewById(R.id.perfect_et_shenfenzheng);
        center_realName = (EditText) findViewById(R.id.perfect_et_realname);
//        center_nickname = (EditText) findViewById(R.id.perfect_et_nickname);

        tiJiao = (TextView) findViewById(R.id.perfect_center_tv_tijiao);
        tiJiao.setOnClickListener(this);
    }

    //
    // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    /**
     * 请求绑定身份证
     */
    private void requestBindIdCard() {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("realName", center_realName.getText().toString());
            request_json.put("idcard", center_shenFenZheng.getText().toString());
            httpPostJson(CommandUtil.USERINFO_IMPROVE, Commands.BINDIDCARD, request_json, true);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        AppToast.getToast().show("完善信息成功");
        this.finish();
    }

    @Override
    protected void onFailure(int requestCode, String errorCode, String message) {
        super.onFailure(requestCode, errorCode, message);
    }

    //
	// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    /**
     * 非空验证
     */
    private boolean isNull(EditText editText) {
        if (editText.getText() == null || editText.getText().toString().equals("")) {
            editText.requestFocus();
            return true;
        }

        return false;
    }

    /**
     * 输入框验证
     *
     * @return
     */
    private boolean et_yanZheng() {
        clear_focus();

        if (isNull(center_shenFenZheng)) {
            center_shenFenZheng.setError("身份证号不能为空！");
            return false;
        }
        //检测身份证号
        center_shenFenZheng.setText(center_shenFenZheng.getText().toString().toUpperCase());
        if (!Pattern.matches("(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)", center_shenFenZheng
				.getText().toString())) {
            center_shenFenZheng.setError("身份证号输入不合法！");
            center_shenFenZheng.requestFocus();
            center_shenFenZheng.setSelectAllOnFocus(true);
            return false;
        }

        if (isNull(center_realName)) {
            center_realName.setError("真实姓名不能为空！");
            return false;
        }

        return true;
    }

    private void clear_focus() {
        center_shenFenZheng.clearFocus();
        center_realName.clearFocus();
//        center_nickname.clearFocus();
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
            case R.id.perfect_center_tv_tijiao:
                if (et_yanZheng()) {
                    requestBindIdCard();
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void onLeftClick() {
        // TODO Auto-generated method stub
        finish();
    }

    @Override
    public void onCenterClick() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRightClick() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onRight2Click() {
        // TODO Auto-generated method stub

    }
}
